<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <!--引入富文本编辑器相关样式文件-->
    <link href="https://cdn.staticfile.org/wangeditor5/5.1.23/css/style.min.css" rel="stylesheet">
    <!--引入Axios-->
    <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>

</head>
<body>
<div id="app">
    <el-container>
        <el-header height="70px" style="background-color: #386582">
            <h1 style="color: white;font-size: 30px;margin: 0;
                            line-height: 70px">
                学维网后台管理系统
                <span style="font-size: 20px;float: right">欢迎{{user.nickname}}回来!
                   <a href="javascript:void(0)" @click="logout()"
                      style="color: orange">退出登录</a>
                </span>
            </h1>
        </el-header>
        <el-container>
            <el-aside width="200px">
                <el-menu @select="handleSelect" default-active="1" style="height: 1000px" active-text-color="blue">
                   <a href="index.html">
                      <img src="http://rvg6mjj7z.hn-bkt.clouddn.com/img/logo.jpg" width="200px" height="200px">
                   </a>
                    <el-divider></el-divider>
                    <!-- 用户管理-->
                    <el-menu-item index="1">
                        <template slot="title">
                            <i class="el-icon-user"></i><span>用户管理</span>
                        </template>
                    </el-menu-item>
                    <!-- 轮播图管理-->
                    <el-menu-item index="2">
                        <template slot="title">
                            <i class="el-icon-picture"></i><span>轮播图</span>
                        </template>
                    </el-menu-item>
                    <!-- 学习资料管理-->
                    <el-menu-item index="3">
                        <template slot="title">
                            <i class="el-icon-s-order"></i><span>动态管理</span>
                        </template>
                    </el-menu-item>
                    <!-- 学习视频管理-->
                    <el-menu-item index="4">
                        <template slot="title">
                            <i class="el-icon-video-camera"></i><span>视频动态管理</span>
                        </template>
                    </el-menu-item>
                    <!-- 学习资讯-->
                    <el-menu-item index="5">
                        <template slot="title">
                            <i class="el-icon-edit"></i><span>付费视频管理</span>
                        </template>
                    </el-menu-item>
                    <el-menu-item index="6">
                        <template slot="title">
                            <i class="el-icon-coin"></i><span>充值</span>
                        </template>
                    </el-menu-item>
                </el-menu>
            </el-aside>
            <el-main>
                <!--用户表格开始-->
                <el-table v-if="currentIndex==1" :data="userArr">
                    <el-table-column type="index" label="编号"></el-table-column>
                    <el-table-column prop="username" label="用户名"></el-table-column>
                    <el-table-column prop="userid" label="用户ID"></el-table-column>
                    <el-table-column prop="nickname" label="昵称"></el-table-column>
                    <el-table-column prop="email" label="邮箱"></el-table-column>
                    <el-table-column prop="mobile" label="电话"></el-table-column>
                    <el-table-column label="头像">
                        <template slot-scope="scope">
                             <el-avatar :src="scope.row.imgUrl"></el-avatar>
                        </template>
                    </el-table-column>
                    <el-table-column label="管理员">
                        <template slot-scope="scope">
                            <el-switch @change="changeAdmin(scope.row)" v-model="scope.row.isAdmin"></el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column prop="created" label="创建时间"></el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="danger"
                                       @click="deleteUser(scope.$index,scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column prop="balance" label="余额"></el-table-column>
                    <el-table-column label="充值">
                        <template slot-scope="scope">
                            <el-button type="success"
                                       @click="recharge(scope.$index,scope.row)">充值</el-button>
                        </template>
                    </el-table-column>

                </el-table>
                <!--用户表格结束-->
                <!--轮播图表格开始-->
                <el-table v-if="currentIndex==2" :data="bannerArr">
                    <el-table-column type="index" label="编号"></el-table-column>
                    <el-table-column label="轮播图">
                        <template slot-scope="scope">
                            <img :src="scope.row.imgUrl" width="200px">
                        </template>
                    </el-table-column>
                    <el-table-column prop="createTime" label="创建时间"></el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="danger"
                                       @click="deleteBanner(scope.$index,scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="success"
                                       @click="location.href='postBanner.html'">添加</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!--轮播图表格结束-->
                <!--动态表格开始-->
                <el-table v-if="currentIndex > 2 && currentIndex <= 4" :data="contentArr" border>
                    <el-table-column label="编号" width="50px" type="index"></el-table-column>
                    <el-table-column label="标题" width="150px"
                                     prop="title" align="center"></el-table-column>
                    <el-table-column label="封面" width="60px">
                        <template slot-scope="scope">
                            <img :src="scope.row.imgUrl" width="50px">
                        </template>
                    </el-table-column>
                    <el-table-column v-if="currentIndex!=4" label="摘要" width="300px"
                                     prop="brief" align="center"></el-table-column>
                    <el-table-column label="类型"
                                     prop="categoryName" align="center"></el-table-column>
                    <el-table-column label="浏览量"
                                     prop="viewCount" align="center"></el-table-column>
                    <el-table-column label="回复量"
                                     prop="commentCount" align="center"></el-table-column>
                    <el-table-column label="创建时间"
                                     prop="createTime" align="center"></el-table-column>
                    <el-table-column label="操作"  width="200px" fixed="right"
                                     align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" type="success"
                                       @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                            <el-button size="mini" type="danger"
                                       @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!--动态表格结束-->
                <!--付费表格开始-->
                <el-table v-if="currentIndex==5" :data="paidCourse" border>
                    <el-table-column label="编号" width="50px"></el-table-column>
                    <el-table-column label="课程名称" width="150px"
                                     prop="coursename" align="center"></el-table-column>
                    <el-table-column label="课程id" width="150px"
                                     prop="bvid" align="center"></el-table-column>
                    <el-table-column label="课程封面" width="60px">
                        <template slot-scope="scope">
                            <img :src="scope.row.imgeurl" width="50px">
                        </template>
                    </el-table-column>
                    <el-table-column label="类型编号"
                                     prop="categoryId" align="center"></el-table-column>
<!--                    <el-table-column label="是否收费"-->
<!--                                     prop="paid" align="center"></el-table-column>-->
                    <el-table-column label="是否收费">
                        <template slot-scope="scope">
                            <el-switch @change="changeFree(scope.row)" v-model="scope.row.paid"></el-switch>
                        </template>
                    </el-table-column>


                    <el-table-column label="价格"
                                     prop="price" align="center"></el-table-column>
                    <el-table-column label="课程简介"
                                     prop="description" align="center"></el-table-column>
                    <el-table-column label="购买人数"
                                     prop="boughtnum" align="center"></el-table-column>
                    <el-table-column label="修改价格"  width="200px" fixed="right"
                                     align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" type="success"
                                       @click="reprice(scope.$index, scope.row)">修改价格</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!--付费表格结束-->
                <!--充值表格开始-->
                <el-table v-if="currentIndex==6" :data="adminRecharge" border>
                    <el-table-column label="编号" width="50px"></el-table-column>
                    <el-table-column label="订单id" width="150px"
                                     prop="id" align="center"></el-table-column>
                    <el-table-column label="用户id" width="150px"
                                     prop="userid" align="center"></el-table-column>
                    <el-table-column label="时间"
                                     prop="time" align="center"></el-table-column>
                    <el-table-column label="金额"
                                     prop="amount" align="center"></el-table-column>
                    <el-table-column label="是否完成"
                                     prop="completed" align="center"></el-table-column>
                    <el-table-column label="订单状态"
                                     prop="status" align="center"></el-table-column>
                    <el-table-column label="充值">
                        <template slot-scope="scope">
                            <el-button type="success"
                                       @click="adminCharge(scope.$index,scope.row)">充值</el-button>
                        </template>
                    </el-table-column>

                </el-table>
                <!--充值表格结束-->
            </el-main>
        </el-container>
        <!--           充值窗口-->
        <el-dialog title="充值" :visible.sync="editFormVisible">
            <el-form  label-width="120px">
                <el-form-item label="充值金额">
                    <el-input v-model="balance"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="updateBalance()">确 定</el-button>
            </div>
        </el-dialog>
        <!--           修改付费课程价格窗口-->
        <el-dialog title="修改价格" :visible.sync="FormVisible">
            <el-form  label-width="120px">
                <el-form-item label="输入价格">
                    <el-input v-model="price"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="FormVisible = false">取 消</el-button>
                <el-button type="primary" @click="updatePrice()">确 定</el-button>
            </div>
        </el-dialog>
    </el-container>
</div>
</body>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                user:JSON.parse(localStorage.user),
                userArr:[],
                bannerArr:[],
                contentArr:[],
                paidCourse:[],
                adminRecharge:[],
                editFormVisible: false,
                FormVisible: false,
                balance: '',
                rechargeid: '',
                price:'',
                repriceid:'',
                currentIndex:"1"
            }
        },
        methods: {
            handleDelete(i,content){
                if(confirm("确定删除此内容吗？")){
                    axios.post("/v1/contents/"+content.id+"/delete").then(function (response){
                        if(response.data.code==1){
                            v.$message.success("删除成功！")
                            v.contentArr.splice(i,1);
                        }
                    })
                }
            },
            handleEdit(i,content){
                location.href="/postArticle.html?id="+content.id;
            },
            deleteBanner(i,banner){
                if (confirm("您确认删除此轮播图吗？")){
                    axios.post("/v1/banners/"+banner.id+"/delete").then(function (response) {
                        if (response.data.code==1){
                            v.bannerArr.splice(i,1);//删除数组中的数据
                        }
                    })
                }
            },
            deleteUser(i,user){
                if(user.userid == v.user.userid){
                    alert("不能删除当前登录的用户");
                    return;
                }
                if(confirm("确认删除此用户吗？")){
                    axios.post("/v1/users/"+user.userid+"/delete")
                        .then(function (response){
                            if(response.data.code ==1){
                                v.userArr.splice(i,1);
                            }
                        })
                }
            },
            changeAdmin(user){
                axios.post("/v1/users/"+user.userid+"/"+(user.isAdmin?1:0)+"/change")
                    .then(function (response){
                        if(response.data.code==1){
                            v.$message.success("修改成功！")
                        }
                    })
            },
            changeFree(paidCourse){
                axios.post("/v1/courses/"+paidCourse.bvid+"/"+(paidCourse.paid?1:0)+"/change")
                    .then(function (response){
                        if(response.data.code==1){
                            v.$message.success("修改成功！")
                        }
                    })
            },
            handleSelect(key,keyPath){
                //key就是点击菜单项的index值
                v.currentIndex = key;
                if(key>=3 && key<5){
                    let type = key-2;
                    console.log(type)
                    axios.get("/v1/contents/"+type+"/admin").then(function (response){
                        if(response.data.code==1)
                        v.contentArr = response.data.data;
                    })
                }
            },
            logout(){
                if(confirm("您确认退出登录吗?")){
                    axios.get("/v1/users/logout").then(function(){
                        localStorage.clear();//清空登录成功时保存的数据
                        location.href="/";
                    })
                }
            },
            recharge(i,user){
                this.editFormVisible = true;
                v.rechargeid = user.userid;
            },
            updateBalance(){
                this.editFormVisible = false;
                this.changebalance();
            },
            changebalance(){
                console.log(v.rechargeid);
                console.log(v.balance);
                axios.post("/v1/users/"+v.rechargeid+"/adminBalance/"+v.balance).then(function (response){
                    if(response.data.code==1){
                        v.$message.success("充值成功!")
                        location.reload();
                    }
                })
            },
            reprice(i,paidCourse){
                this.FormVisible = true;
                v.repriceid = paidCourse.bvid;
            },
            updatePrice(){
                this.FormVisible = false;
                this.changeprice();
            },
            changeprice(){
                axios.post("/v1/courses/"+v.repriceid+"/adminPrice/"+v.price).then(function (response){
                    if(response.data.code==1){
                        v.$message.success("修改成功!")
                        location.reload();
                    }
                })
            },
            adminCharge(i,adminRecharge){
                console.log(adminRecharge.userid);
                console.log(adminRecharge.amount);
                console.log(adminRecharge.id)
                axios.post("/v1/recharge/"+adminRecharge.userid+"/adminRecharge/"+adminRecharge.amount+"/"+adminRecharge.id).then(function (response){
                    if(response.data.code==1){
                        v.$message.success("修改成功!")
                        location.reload();
                    }
                })
            },

        },
        created:function () {
            let user = JSON.parse(localStorage.user);
            if (user.isAdmin != 1) {
                 alert("请登录管理员账户！");
                location.href = "/login.html";
            }
            //请求用户列表
            axios.get("/v1/users/").then(function (response){
                if(response.data.code==1){
                    v.userArr = response.data.data;
                }
            })
            //请求轮播图
            axios.get("/v1/banners/admin").then(function (response){
                if(response.data.code==1){
                    v.bannerArr = response.data.data;
                }
            })
            //请求付费课程
            axios.get("/v1/courses/paidAdmin").then(function (response){
                if(response.data.code==1){
                    v.paidCourse = response.data.data;
                }
            })
            //请求充值界面信息
            axios.get("/v1/recharge/admin").then(function (response){
                if(response.data.code==1){
                    v.adminRecharge = response.data.data;
                }
            })
        }
    })
</script>
</html>